/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ui.extra;

import java.awt.Color;
import java.awt.Graphics2D;

import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.Orientation;
import org.apache.pivot.wtk.skin.ComponentSkin;

/**
 * Gestionnaire de style du ruban affichant les numeros des lignes dans l'editeur
 * @author Inspire d'un code realise par les developpeurs de Pivot
 */
class RulerSkin extends ComponentSkin implements RulerListener {

	/**
	 * {@inheritDoc}
	 */
    public void install(Component component) {
        super.install(component);

        Ruler ruler = (Ruler)component;
        ruler.getRulerListeners().add(this);
    }

    /**
	 * {@inheritDoc}
	 */
    public void layout() {
        // No-op
    }

    /**
	 * {@inheritDoc}
	 */
    public int getPreferredHeight(int width) {
        Ruler ruler = (Ruler)getComponent();
        Orientation orientation = ruler.getOrientation();

        return (orientation == Orientation.HORIZONTAL) ? 20 : 0;
    }

    /**
	 * {@inheritDoc}
	 */
    public int getPreferredWidth(int height) {
        Ruler ruler = (Ruler)getComponent();
        Orientation orientation = ruler.getOrientation();

        return (orientation == Orientation.VERTICAL) ? 35 : 0;
    }

    /**
	 * {@inheritDoc}
	 */
    public void paint(Graphics2D graphics) {
        int width = getWidth();
        int height = getHeight();

        Ruler ruler = (Ruler)getComponent();

        graphics.setColor(new Color(0xE4, 0xE4, 0xE4));
        graphics.fillRect(0, 0, width, height);

        graphics.setColor(new Color(0x99, 0x99, 0x99));
        graphics.drawRect(-1, -1, width + 1, height + 1);

        graphics.setColor(new Color(0x80, 0x80, 0x80));
        
        Orientation orientation = ruler.getOrientation();
        switch (orientation) {
            case HORIZONTAL: {
                for (int i = 0, n = width / 5 + 1; i < n; i++) {
                    int x = i * 5;

                    if (i % 4 == 0) {
                        graphics.drawLine(x, 0, x, height / 2);
                    } else {
                        graphics.drawLine(x, 0, x, height / 4);
                    }
                }

                break;
            }

            case VERTICAL: {
            	Integer numLine = 1;
                for (int i = 0, n = (int) (height / 3.5); i < n-2; i++) {
                    int y = (int) (i * 3.5);

                    if (i % 4 == 0) {
                    	String line = numLine.toString();
                    	
                    	graphics.drawString(line, 25-(7*(line.length()-1)), y+14);
                    	numLine++;
                    } else {
                       // On ne fait rien
                    }
                }

                break;
            }
        }
        graphics.setColor(new Color(0x99, 0x99, 0x99));
        graphics.drawRect(-1, -1, width, height + 1);
    }

    /**
	 * {@inheritDoc}
	 */
    public void orientationChanged(Ruler ruler) {
        invalidateComponent();
    }
}